package com.ubxtech.rabbitmq.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ubxtech.rabbitmq.bo.RabbitmqEventBO;
import com.ubxtech.rabbitmq.service.RabbitmqService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Victor.Xiao
 * @since 2023-05-15 10:46
*/
@Slf4j
@Service
public class RabbitmqServiceImpl implements RabbitmqService {

    @Autowired(required = false)
    private RabbitTemplate rabbitTemplate;



    /**
     * 发送mq
     * @author Victor.Xiao
     * @since 2023-05-30 11:16
     * @param rabbitmqEventBO rabbitmqEventBO
     */
    @Override
    public void convertAndSend(RabbitmqEventBO rabbitmqEventBO) {
        if (rabbitTemplate == null) {
            if (log.isDebugEnabled()) {
                log.debug("发送rabbitmq事件 没有初始化rabbitTemplate:{}", rabbitmqEventBO);
            }
            return;
        }
        if (rabbitmqEventBO.getMessage() == null) {
            log.error("发送rabbitmq事件object 为空 handleEvent:{}", rabbitmqEventBO);
            return;
        }
        if (StrUtil.isNotBlank(rabbitmqEventBO.getExchange()) && StrUtil.isNotBlank(rabbitmqEventBO.getRoutingKey())) {
            rabbitTemplate.convertAndSend(rabbitmqEventBO.getExchange(), rabbitmqEventBO.getRoutingKey(), rabbitmqEventBO.getMessage());
        } else if (StrUtil.isNotBlank(rabbitmqEventBO.getRoutingKey())) {
            rabbitTemplate.convertAndSend(rabbitmqEventBO.getRoutingKey(), rabbitmqEventBO.getMessage());
        } else {
            rabbitTemplate.convertAndSend(rabbitmqEventBO.getMessage());
        }
    }
}
